package com.kingdee.eas.jky.pm.app;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.fi.ar.IOtherBill;
import com.kingdee.eas.fi.ar.OtherBillFactory;
import com.kingdee.eas.fi.ar.OtherBillInfo;
import com.kingdee.eas.jky.enums.HXStateEnum;
import com.kingdee.eas.jky.pm.EarnHxBillE1Info;
import com.kingdee.eas.jky.pm.EarnHxBillE2Info;
import com.kingdee.eas.jky.pm.EarnHxBillE3Info;
import com.kingdee.eas.jky.pm.EarnHxBillFactory;
import com.kingdee.eas.jky.pm.EarnHxBillInfo;
import com.kingdee.eas.jky.pm.EarningAffirmFactory;
import com.kingdee.eas.jky.pm.EarningAffirmInfo;
import com.kingdee.eas.jky.pm.IEarningAffirm;

public class EarnHxBillControllerBean extends AbstractEarnHxBillControllerBean
{
    private static Logger logger =
        Logger.getLogger("com.kingdee.eas.jky.pm.app.EarnHxBillControllerBean");
    protected void _audit(Context ctx, IObjectPK pk)throws BOSException, EASBizException
    {
         super._audit(ctx, pk);
         EarnHxBillInfo EarnHxInfo = (EarnHxBillInfo) this.getValue(ctx, pk);
         IEarningAffirm EarningAffirm =EarningAffirmFactory.getLocalInstance(ctx);
         IOtherBill   OtherBill=OtherBillFactory.getLocalInstance(ctx);
         if(EarnHxInfo.getE2().size()>0){
        	 
        	 for(int i=0;i<EarnHxInfo.getE2().size();i++){
        		 EarnHxBillE2Info  E2Info= EarnHxInfo.getE2().get(i); 
             	if(E2Info.getSourceID()!=null){
             		EarningAffirmInfo earnInfo =EarningAffirm.getEarningAffirmInfo(new ObjectUuidPK(E2Info.getSourceID()));
             		earnInfo.setNothxAmount(earnInfo.getNothxAmount().subtract(EarnHxInfo.getYshsAmount()));
             		earnInfo.setYhxAmount(earnInfo.getYhxAmount().add(EarnHxInfo.getYshsAmount()));
             		if(earnInfo.getYhxAmount().compareTo(earnInfo.getBqbhsAmount())==0){
             		 earnInfo.setHxState(HXStateEnum.yhx);	
             		}else{
             		 earnInfo.setHxState(HXStateEnum.bfhx);		
             		}
             		EarningAffirm.update(new ObjectUuidPK(E2Info.getSourceID()), earnInfo);
             	}
        	 }
        	 for(int i=0;i<EarnHxInfo.getE1().size();i++){
        		 EarnHxBillE1Info  E1Info= EarnHxInfo.getE1().get(i); 
        		 if(E1Info.getSourceID()!=null){
        			OtherBillInfo info =OtherBill.getOtherBillInfo(new ObjectUuidPK(E1Info.getSourceID()));
        			info.setString("isHx", "1");
        			OtherBill.update(new ObjectUuidPK(E1Info.getSourceID()), info);
        		 }
        	 }
         }
         if(EarnHxInfo.getE3().size()>0){
        	 for(int i=0;i<EarnHxInfo.getE3().size();i++){
        		 EarnHxBillE3Info  E3Info= EarnHxInfo.getE3().get(i); 
             	if(E3Info.getSourceID()!=null){
             		EarningAffirmInfo earnInfo =EarningAffirm.getEarningAffirmInfo(new ObjectUuidPK(E3Info.getSourceID()));
             		earnInfo.setNothxAmount(earnInfo.getNothxAmount().add(EarnHxInfo.getYshsAmount()));
             		earnInfo.setYhxAmount(earnInfo.getYhxAmount().subtract(EarnHxInfo.getYshsAmount()));
             		if(earnInfo.getYhxAmount().compareTo(earnInfo.getBqbhsAmount())==0){
             		 earnInfo.setHxState(HXStateEnum.yhx);	
             		}else{
             		 earnInfo.setHxState(HXStateEnum.bfhx);		
             		}
             		EarningAffirm.update(new ObjectUuidPK(E3Info.getSourceID()), earnInfo);
             	}
        	 }
         }
         EarnHxInfo.setHxState(HXStateEnum.yhx);
         EarnHxBillFactory.getLocalInstance(ctx).update(pk, EarnHxInfo);
        	 
    }
    protected void _unAudit(Context ctx, IObjectPK pk)throws BOSException, EASBizException
    {
         super._unAudit(ctx, pk);
         EarnHxBillInfo EarnHxInfo = (EarnHxBillInfo) this.getValue(ctx, pk);
         IEarningAffirm EarningAffirm =EarningAffirmFactory.getLocalInstance(ctx);
         IOtherBill   OtherBill=OtherBillFactory.getLocalInstance(ctx);
         if(EarnHxInfo.getE2().size()>0){
        	 for(int i=0;i<EarnHxInfo.getE2().size();i++){
        		   	EarnHxBillE2Info  E2Info= EarnHxInfo.getE2().get(i); 
                	if(E2Info.getSourceID()!=null){
                		EarningAffirmInfo earnInfo =EarningAffirm.getEarningAffirmInfo(new ObjectUuidPK(E2Info.getSourceID()));
                		earnInfo.setNothxAmount(earnInfo.getNothxAmount().add(EarnHxInfo.getYshsAmount()));
                		earnInfo.setYhxAmount(earnInfo.getYhxAmount().subtract(EarnHxInfo.getYshsAmount()));
                		if(earnInfo.getNothxAmount().compareTo(earnInfo.getBqbhsAmount())==0){
                		 earnInfo.setHxState(HXStateEnum.whx);	
                		}else{
                		 earnInfo.setHxState(HXStateEnum.bfhx);		
                		}
                		EarningAffirm.update(new ObjectUuidPK(E2Info.getSourceID()), earnInfo);
                	}
        	 }
         }
         for(int i=0;i<EarnHxInfo.getE1().size();i++){
    		 EarnHxBillE1Info  E1Info= EarnHxInfo.getE1().get(i); 
    		 if(E1Info.getSourceID()!=null){
    			OtherBillInfo info =OtherBill.getOtherBillInfo(new ObjectUuidPK(E1Info.getSourceID()));
    			info.setString("isHx", "0");
    			OtherBill.update(new ObjectUuidPK(E1Info.getSourceID()), info);
    		 }
    	 }
        	 if(EarnHxInfo.getE3().size()>0){
            	 for(int i=0;i<EarnHxInfo.getE3().size();i++){
            		   	EarnHxBillE3Info  E3Info= EarnHxInfo.getE3().get(i); 
                    	if(E3Info.getSourceID()!=null){
                    		EarningAffirmInfo earnInfo =EarningAffirm.getEarningAffirmInfo(new ObjectUuidPK(E3Info.getSourceID()));
                    		earnInfo.setNothxAmount(earnInfo.getNothxAmount().subtract(EarnHxInfo.getYshsAmount()));
                    		earnInfo.setYhxAmount(earnInfo.getYhxAmount().add(EarnHxInfo.getYshsAmount()));
                    		if(earnInfo.getNothxAmount().compareTo(earnInfo.getBqbhsAmount())==0){
                    		 earnInfo.setHxState(HXStateEnum.whx);	
                    		}else{
                    		 earnInfo.setHxState(HXStateEnum.bfhx);		
                    		}
                    		EarningAffirm.update(new ObjectUuidPK(E3Info.getSourceID()), earnInfo);
                    	}
            	 }
         }
         EarnHxInfo.setHxState(HXStateEnum.whx);
         EarnHxBillFactory.getLocalInstance(ctx).update(pk, EarnHxInfo);
    }
    protected void _unAudit(Context ctx, IObjectPK[] pks)throws BOSException, EASBizException
    {
         super._unAudit(ctx, pks);
    }
    protected String _getBindingProperty(Context ctx)throws BOSException
    {
        return  super._getBindingProperty(ctx);
    }
}